<?php 
    function checkData($username, $email){
        $check = array();
        $db = Database::getInstance();
        $query = "SELECT * FROM users WHERE email = '$email'";
        $result = $db->query($query);
        if($result->dataTable->numOfRow > 0)
            $check["email"] = "Email";
        $query = "SELECT * FROM users WHERE username = '$username'";
        $result = $db->query($query);
        if($result->dataTable->numOfRow > 0)
            $check["username"] = "Username";
        return $check;
    }
    
    if (isset($_POST['user']) && sizeof(checkData($_POST['user']['login'], $_POST['user']['email'])) == 0) {
        $user = $_POST['user'];
        User::addUser($user);
    }
    else {
        $check = checkData($_POST['user']['login'], $_POST['user']['email']);
        $_POST["commit"] = "";
        if($check["username"] != "")
            $_POST["commit"] = $check["username"]." existed.";
        if($check["email"] != "")
            $_POST["commit"] = $_POST["commit"].$check["email"]." existed.";
?>
<script>
    function validate(){
        var submit = true;
        var user_name = document.forms[1].user_name;
        if(user_name.value === ""){
            user_name.previousSibling.style.color = 'red';
            submit = false;
        }else
            user_name.previousSibling.style.color = 'black';
        var user_login = document.forms[1].user_login;
        if(user_login.value === ""){
            user_login.previousSibling.style.color = 'red';
            submit = false;
        }else{
            var patt = new RegExp("^[a-z0-9_-]{3,15}$");
            if(patt.test(user_login.value))
                switch(user_login.value.toUpperCase()){
                    case 'SHOTS':
                    case 'POPULAR':
                    case 'EVERYONE':
                    case 'DEBUTS':
                    case 'EXPLORE':
                    case 'HIGHLIGHTS':
                    case 'COLORS':
                    case 'ABOUT':
                    case 'SIGNUP':
                    case 'LOGIN':
                        user_login.previousSibling.style.color = 'red';
                        submit = false;
                        break;
                    default:
                        user_login.previousSibling.style.color = 'black';
                        document.getElementById('error').style.display = 'none';
                        break;
                }
            else{
                document.getElementById('error').style.display = 'inherit';
                submit = false;
            }
        }
        var user_email = document.forms[1].user_email;
        if(user_email.value === ""){
            user_email.previousSibling.style.color = 'red';
            submit = false;
        }else
            user_email.previousSibling.style.color = 'black';
        var user_password = document.forms[1].user_password;
        if(user_password.value === ""){
            user_password.previousSibling.style.color = 'red';
            submit = false;
        }else if(user_password.value.length < 6){
                user_password.previousSibling.style.color = 'red';
                submit = false;
                }else
                    user_password.previousSibling.style.color = 'black';
        var user_address = document.forms[1].user_address;
        if(user_address.value === ""){
            user_address.previousSibling.style.color = 'red';
            submit = false;
        }else
            user_address.previousSibling.style.color = 'black';
        if(submit){
            document.getElementById('error_exist').style.display = "none";
            document.forms[1].submit();
        }
    }
    
    function AuthorizeTwitter(){
        window.open("authorizeTwitter");
    }

    $(window).focus(function() {
        var user_name = document.forms[1].user_name;
        user_name.value = "<?php echo $_SESSION['twitter']->name; ?>";
        if(user_name.value === ""){
            parent.window.location.reload(true);
        }
        console.log('<?php echo $_SESSION['twitter']->name; ?>');
        var user_login = document.forms[1].user_login;
        user_login.value = "<?php echo $_SESSION['twitter']->screen_name; ?>";
        var user_address = document.forms[1].user_address;
        user_address.value = "<?php echo $_SESSION['twitter']->location; ?>";
    });
</script>
<div class="col-about col-about-full under-hero">

    <h2 class="alt">ySocial is a show and tell community for designers</h2>
    <h3 class="sub">
        Sign up to find, follow and hire members of our small but vibrant community of designers, icon artists, illustrators, interface designers, typographers, and other creative folks.
    </h3>

    <div class="signup-twitter">
        <a href="" onclick="return AuthorizeTwitter();" class="auth-twitter auth-top">Sign up with Twitter</a> or sign up below:
    </div>

    <div class="signup-form">
        <form onsubmit="return false;" accept-charset="UTF-8" action="" class="hide-remove gen-form with-messages signup" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="tOXS7KxE6BEbVoZpy8rc9v8v72iwMtvWLEHtTEc1ygk="></div>
            <div class="errorExplanation" id="errorExplanation" style="display: none;">
            <h2>Clank!</h2><ul><li>Username can't be blank</li><li>Name can't be blank</li><li>Email can't be blank</li><li>Password can't be blank</li></ul></div>
            <input id="user_prospect" name="user[prospect]" type="hidden" value="false">
            <div class="form-field">
                <fieldset class="user_name"><label for="user_name">Name</label><input id="user_name" name="user[name]" size="30" type="text" title="" value="<?php if(isset($_SESSION['twitter'])) echo $_SESSION['twitter']->name; ?>"></fieldset>
                <p class="message">We're big on real names around here, so people know who's who</p>
            </div>
            <div class="form-field">

                <fieldset class="user_login"><label for="user_login">Username</label><input autocapitalize="off" autocorrect="off" id="user_login" name="user[login]" size="30" type="text" value="<?php if(isset($_SESSION['twitter'])) echo $_SESSION['twitter']->screen_name; ?>"></fieldset>
                <p class="message">
                    Your ySocial URL: http://ySocial.com/<strong><span id="username">USERNAME</span></strong>
                </p>
            </div>
            <div class="form-field">
                <fieldset class="user_email"><label for="user_email">Email</label><input id="user_email" name="user[email]" size="30" type="text"</fieldset>
            </div>
            <div class="form-field">
                <fieldset class="user_password"><label for="user_password">Password</label><input id="user_password" name="user[password]" size="30" type="password"></fieldset>
                <p class="message">Minimum 6 characters</p>
            </div>
            <div class="form-field">
                <fieldset class="user_address"><label for="user_address">Address</label><input id="user_address" name="user[address]" size="30" type="text" value="<?php if(isset($_SESSION['twitter'])) echo $_SESSION['twitter']->location; ?>"></fieldset>
            </div>
            <div id="error" style="color: red; font-family: Calibri; font-size: 11pt;padding-bottom: 5px;display: none;margin-left: 135px;" align="center">
                Username may only contain A-Z, 0-9, underscores, dashes and periods
            </div>
            <div id="error_exist" style="width: 100%; margin-right: 143px; color: red; font-family: Calibri; font-size: 11pt;padding-bottom: 5px;<?php if(isset($_POST['user']) && $_POST['commit'] != "") echo "display: inherit;"; else echo "display: none;"; ?>" align="center">
                <?php echo $_POST["commit"]; ?>
            </div>
            <div class="form-btns">
                <input class="form-sub" name="commit" type="submit" value="Create Account" onclick="validate();">
            </div>
        </form>    
    </div>
</div>
<?php } ?>